﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace TimKen.DataAccess
{
    /// <summary>
    /// 数据访问类Resource。
    /// </summary>
    public class ResourceDAO
    {
        public ResourceDAO()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        //public int GetMaxId()
        //{
        //    return DbHelperSQL.GetMaxID("FileName", "Resource");
        //}

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string  FileName)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@FileName", SqlDbType.NVarChar,200)};
            parameters[0].Value = FileName;

            int result = DbHelperSQL.RunProcedure("UP_Resource_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public void Add(TimKen.DataEntity.Resource model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@FileName", SqlDbType.VarChar,200),
					new SqlParameter("@DescriptCN", SqlDbType.VarChar,5000),
					new SqlParameter("@DescriptEN", SqlDbType.VarChar,5000)
                    ,new SqlParameter("@ENName", SqlDbType.VarChar,5000)
                    ,new SqlParameter("@FileSize", SqlDbType.VarChar,5000)
                    ,new SqlParameter("@Route", SqlDbType.VarChar,5000)};    
         
            parameters[0].Value = model.FileName;
           
            parameters[1].Value = model.DescriptCN;
            parameters[2].Value = model.DescriptEN;
            parameters[3].Value = model.Enname;
            parameters[4].Value = model.Filesize;
            parameters[5].Value = model.Route;

            DbHelperSQL.RunProcedure("UP_Resource_ADD", parameters, out rowsAffected);
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.Resource model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					
					new SqlParameter("@FileName", SqlDbType.VarChar,200),
					
					new SqlParameter("@DescriptCN", SqlDbType.VarChar,5000),
					new SqlParameter("@DescriptEN", SqlDbType.VarChar,5000)
                    ,new SqlParameter("@ENName", SqlDbType.VarChar,5000)
                    ,new SqlParameter("@FileSize", SqlDbType.VarChar,5000)
                    ,new SqlParameter("@Route", SqlDbType.VarChar,5000)};    
           
            parameters[0].Value = model.FileName;
            
            parameters[1].Value = model.DescriptCN;
            parameters[2].Value = model.DescriptEN;
            parameters[3].Value = model.Enname;
            parameters[4].Value = model.Filesize;
            parameters[5].Value = model.Route;

            DbHelperSQL.RunProcedure("UP_Resource_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string  FileName)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@FileName", SqlDbType.NVarChar,200)};
            parameters[0].Value = FileName;

            DbHelperSQL.RunProcedure("UP_Resource_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.Resource GetModel(string   FileName)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@FileName", SqlDbType.NVarChar,200)};
            parameters[0].Value = FileName;

            TimKen.DataEntity.Resource model = new TimKen.DataEntity.Resource();
            DataSet ds = DbHelperSQL.RunProcedure("UP_Resource_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
               
                model.FileName = ds.Tables[0].Rows[0]["FileName"].ToString();
               
                model.DescriptCN = ds.Tables[0].Rows[0]["DescriptCN"].ToString();
                model.DescriptEN = ds.Tables[0].Rows[0]["DescriptEN"].ToString();

                model.Enname = ds.Tables[0].Rows[0]["ENName"].ToString();           
                model.Filesize = ds.Tables[0].Rows[0]["FileSize"].ToString();
                model.Route = ds.Tables[0].Rows[0]["Route"].ToString();                                         
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,FileName,Url,DescriptCN,DescriptEN ");
            strSql.Append(" FROM Resource ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

     
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Resource";
            parameters[1].Value = "FileName";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }


        public DataSet GetPageList(int pageSize, int PageIndex,string fileName)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@FileName", SqlDbType.NVarChar,200),
					new SqlParameter("@PageSize", SqlDbType.Int,4),
                    new SqlParameter("@PageIndex", SqlDbType.Int,4)};
            parameters[0].Value = fileName;
            parameters[1].Value = pageSize;
            parameters[2].Value = PageIndex;
            return DbHelperSQL.RunProcedure("UP_Resource_GetPageList", parameters, "Resource");

        }

        public DataSet GetPrdctFilePageList(int pageSize, int PageIndex, string fileName)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@FileName", SqlDbType.NVarChar,200),
					new SqlParameter("@PageSize", SqlDbType.Int,4),
                    new SqlParameter("@PageIndex", SqlDbType.Int,4)};
            parameters[0].Value = fileName;
            parameters[1].Value = pageSize;
            parameters[2].Value = PageIndex;
            return DbHelperSQL.RunProcedure("UP_Resource_GetPrdctFilePageList", parameters, "Resource");
        }
        #endregion  成员方法
    }
}
